package com.mediacloud.app.newsmodule.view

import android.content.Context
import android.graphics.drawable.Drawable
import androidx.appcompat.widget.AppCompatImageView
import androidx.appcompat.widget.LinearLayoutCompat


class IndicatorView(context:Context):LinearLayoutCompat(context) {

    private var selectColor: Drawable? = null
    private var normalColor: Drawable? = null
    private var afp:Float = 0f
    fun init(indicatorCount:Int,selectPosition:Int, selectDrawble:Drawable, normalDrawble:Drawable,alp:Float):IndicatorView{
        selectColor = selectDrawble
        normalColor = normalDrawble
        afp = alp
        for (i in 0 until indicatorCount) {
            val ivIndicator = AppCompatImageView(context)
            val lp = LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)
            lp.leftMargin = if (i == 0) 0 else dp2px(context,4).toInt()
            ivIndicator.layoutParams = lp
            ivIndicator.background = if (i == selectPosition) selectDrawble else normalDrawble
            ivIndicator.alpha = if (i == selectPosition)  1f else alp
            addView(ivIndicator)
        }
        return this
    }

    fun changeIndicator(position: Int) {
        val count = childCount
        for (i in 0 until count) {
            val ivIndecator = getChildAt(i) as AppCompatImageView
            ivIndecator.background = normalColor
            ivIndecator.alpha = afp
        }
        val ivIndecator = getChildAt(position) as AppCompatImageView
        ivIndecator.background = selectColor
        ivIndecator.alpha = 1.0f
    }


    fun dp2px(context: Context, dpValue: Int): Float {
        val scale = context.resources.displayMetrics.density
        return dpValue * scale + 0.5f
    }
}